package com.ruoyi.honor.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.honor.mapper.SysHonorRewardMapper;
import com.ruoyi.honor.mapper.SysHonorMemberMapper;
import com.ruoyi.honor.domain.SysHonorReward;
import com.ruoyi.honor.domain.SysHonorMember;
import com.ruoyi.honor.service.ISysHonorRewardService;

/**
 * 荣誉奖励Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@Service
public class SysHonorRewardServiceImpl implements ISysHonorRewardService 
{
    @Autowired
    private SysHonorRewardMapper sysHonorRewardMapper;

    @Autowired
    private SysHonorMemberMapper sysHonorMemberMapper;

    /**
     * 查询荣誉奖励
     * 
     * @param honorId 荣誉奖励主键
     * @return 荣誉奖励
     */
    @Override
    public SysHonorReward selectSysHonorRewardByHonorId(Long honorId)
    {
        return sysHonorRewardMapper.selectSysHonorRewardByHonorId(honorId);
    }

    /**
     * 查询荣誉奖励列表
     * 
     * @param sysHonorReward 荣誉奖励
     * @return 荣誉奖励
     */
    @Override
    public List<SysHonorReward> selectSysHonorRewardList(SysHonorReward sysHonorReward)
    {
        return sysHonorRewardMapper.selectSysHonorRewardList(sysHonorReward);
    }

    /**
     * 新增荣誉奖励
     * 
     * @param sysHonorReward 荣誉奖励
     * @return 结果
     */
    @Override
    public int insertSysHonorReward(SysHonorReward sysHonorReward)
    {
        sysHonorReward.setCreateTime(DateUtils.getNowDate());
        return sysHonorRewardMapper.insertSysHonorReward(sysHonorReward);
    }

    /**
     * 修改荣誉奖励
     * 
     * @param sysHonorReward 荣誉奖励
     * @return 结果
     */
    @Override
    public int updateSysHonorReward(SysHonorReward sysHonorReward)
    {
        sysHonorReward.setUpdateTime(DateUtils.getNowDate());
        return sysHonorRewardMapper.updateSysHonorReward(sysHonorReward);
    }

    /**
     * 批量删除荣誉奖励
     * 
     * @param honorIds 需要删除的荣誉奖励主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysHonorRewardByHonorIds(Long[] honorIds)
    {
        // 删除关联的人员信息
        for (Long honorId : honorIds)
        {
            sysHonorMemberMapper.deleteSysHonorMemberByHonorId(honorId);
        }
        return sysHonorRewardMapper.deleteSysHonorRewardByHonorIds(honorIds);
    }

    /**
     * 删除荣誉奖励信息
     * 
     * @param honorId 荣誉奖励主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysHonorRewardByHonorId(Long honorId)
    {
        // 删除关联的人员信息
        sysHonorMemberMapper.deleteSysHonorMemberByHonorId(honorId);
        return sysHonorRewardMapper.deleteSysHonorRewardByHonorId(honorId);
    }

    /**
     * 根据年份统计荣誉数量
     * 
     * @param year 年份
     * @return 结果
     */
    @Override
    public int countHonorByYear(String year)
    {
        return sysHonorRewardMapper.countHonorByYear(year);
    }

    /**
     * 根据级别统计荣誉数量
     * 
     * @param level 级别
     * @return 结果
     */
    @Override
    public int countHonorByLevel(String level)
    {
        return sysHonorRewardMapper.countHonorByLevel(level);
    }

    /**
     * 保存荣誉奖励及关联人员
     * 
     * @param sysHonorReward 荣誉奖励
     * @param membersIds 关联人员ID数组
     * @return 结果
     */
    @Override
    @Transactional
    public int saveHonorRewardWithMembers(SysHonorReward sysHonorReward, Long[] membersIds)
    {
        int result = 0;
        if (sysHonorReward.getHonorId() == null)
        {
            // 新增荣誉奖励
            result = insertSysHonorReward(sysHonorReward);
        }
        else
        {
            // 修改荣誉奖励
            result = updateSysHonorReward(sysHonorReward);
            // 删除原有关联人员
            sysHonorMemberMapper.deleteSysHonorMemberByHonorId(sysHonorReward.getHonorId());
        }

        // 新增关联人员
        if (StringUtils.isNotNull(membersIds) && membersIds.length > 0)
        {
            for (Long membersId : membersIds)
            {
                SysHonorMember honorMember = new SysHonorMember();
                honorMember.setHonorId(sysHonorReward.getHonorId());
                honorMember.setMembersId(membersId);
                sysHonorMemberMapper.insertSysHonorMember(honorMember);
            }
        }
        return result;
    }

    /**
     * 根据荣誉ID查询关联人员列表
     * 
     * @param honorId 荣誉ID
     * @return 人员列表
     */
    @Override
    public List<SysHonorMember> selectMembersByHonorId(Long honorId)
    {
        return sysHonorMemberMapper.selectMembersByHonorId(honorId);
    }
}
